import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-11-10
 * Time: 19:31
 */
public class RedisDemoSet {

    public static void test1(Jedis jedis) {
        System.out.println("sadd 和 smembers");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        // set 为无序的
        Set<String> result = jedis.smembers("key");
        System.out.println("result:" + result);

    }


    public static void test2(Jedis jedis) {
        System.out.println("sismember");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        boolean result = jedis.sismember("key", "1111");
        System.out.println("result:" + result);

    }

    public static void test3(Jedis jedis) {
        System.out.println("scard");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        // 元素个数
        long result = jedis.scard("key");
        System.out.println("result:" + result);

    }

    public static void test4(Jedis jedis) {
        System.out.println("spop");
        jedis.flushAll();


        jedis.sadd("key", "111", "222", "333");
        // 随机删除一个元素
        String result = jedis.spop("key");
        System.out.println("result:" + result);
    }

    public static void test5(Jedis jedis) {
        System.out.println("sinter");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        jedis.sadd("key2", "111", "222", "444");

        Set<String> result = jedis.sinter("key", "key2");
        System.out.println("result:" + result);
    }


    public static void test6(Jedis jedis) {
        System.out.println("sinterstore");
        jedis.flushAll();

        jedis.sadd("key", "111", "222", "333");
        jedis.sadd("key2", "111", "222", "444");

        long len = jedis.sinterstore("key3", "key", "key2");
        System.out.println("len:" + len);

        Set<String> result = jedis.smembers("key3");
        System.out.println("result:" + result);
    }

        public static void main(String[] args) {
        try (JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888")) {
            Jedis jedis = jedisPool.getResource();
            test6(jedis);

        }

    }
}
